﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Aspose.Cells;
using Web.SystemConfig.WorkFlow;
using System.Configuration;
using System.IO;

namespace Web
{
    public class AsposeExprtToExcel
    {
        /// <summary>
        /// 导出到excel（通过Http方式下载）
        /// </summary>
        /// <param name="ds">数据源，多个sheet对应多个DataTable </param>
        /// <param name="templetePath">导出模板的路径</param>
        /// <param name="response">HttpResponse对象</param>
        /// <param name="tsBookName">导出的Excel名称</param>
        public static void ExportToExcel(DataSet ds, string templetePath, string tsBookName, HttpResponse response)
        {
            Aspose.Cells.WorkbookDesigner designer = new WorkbookDesigner();
            //给模板对象设置数据源
            designer.SetDataSource(ds);
            //excel模板的地址
            string excelModel = templetePath;
            designer.Open(excelModel);
            designer.Process();//全自动赋值
            //新生成的excel的保存地址
            //以下是保存到物理地址方法
            //string fileToSave = @"D:\create1.xls";
            //if (System.IO.File.Exists(fileToSave) == true)
            //{
            //    System.IO.File.Delete(fileToSave);
            //}
            //designer.Save(fileToSave);

            //以下是输出到HttpResponse下载
            designer.Save(HttpUtility.UrlEncode(tsBookName + ".xls"), SaveType.OpenInExcel, FileFormatType.Default, response);
        }

        /// <summary>
        /// 导出到excel（下载到指定地址）
        /// </summary>
        /// <param name="ds">数据源，多个sheet对应多个DataTable </param>
        /// <param name="templetePath">导出模板的路径</param>
        /// <param name="fileToSave">导出到的路径</param>
        public static void ExportToExcel(DataSet ds, string templetePath, string fileToSave)
        {
            Aspose.Cells.WorkbookDesigner designer = new WorkbookDesigner();
            //给模板对象设置数据源
            designer.SetDataSource(ds);
            //excel模板的地址
            string excelModel = templetePath;
            designer.Open(excelModel);
            designer.Process();//全自动赋值
            //以下是输出到文件地址下载
            if (System.IO.File.Exists(fileToSave) == true)
            {
                System.IO.File.Delete(fileToSave);
            }
            designer.Save(fileToSave);
        }


        /// <summary>
        /// 根据档案类别获取导出模板路径
        /// </summary>
        /// <param name="archiveType">档案类别（子类别）</param>
        /// <returns></returns>
        public static string GetTempletePath(int archiveType)
        {
            //目录
            string templetePath = "PLuploads/DownloadTemplete/";
            //模板文件名
            string tempFile = string.Empty;
            //如果配置文件配置了基目录，读取配置文件中的
            if (ConfigurationManager.AppSettings.AllKeys.Contains("DownloadTempletePath") &&
                !string.IsNullOrEmpty(ConfigurationManager.AppSettings["DownloadTempletePath"]))
            {
                templetePath = ConfigurationManager.AppSettings["DownloadTempletePath"];
            }
            switch (archiveType)
            {
                case (int)ArchiveTableEnum.AccountI01:
                    tempFile = "会计凭证.xls";
                    break;
                case (int)ArchiveTableEnum.AccountI02:
                    tempFile = "会计账簿.xls";
                    break;
                case (int)ArchiveTableEnum.AccountI03:
                    tempFile = "财务报告.xls";
                    break;
                case (int)ArchiveTableEnum.AccountI04:
                    tempFile = "会计其他.xls";
                    break;
                case (int)ArchiveTableEnum.Document:
                    tempFile = "文书件.xls";
                    break;
                case (int)ArchiveTableEnum.Document_Archive_Vol:
                    tempFile = "文书卷.xls";
                    break;
                case (int)ArchiveTableEnum.Document_Archive_Vol_File:
                    tempFile = "文书卷内件.xls";
                    break;
                case (int)ArchiveTableEnum.BulidProject:
                    tempFile = "基建.xls";
                    break;
                case (int)ArchiveTableEnum.OliSearchK01:
                    tempFile = "勘探（综合）.xls";
                    break;
                case (int)ArchiveTableEnum.OliSearch_Zonghe_Vol:
                    tempFile = "综合卷.xls";
                    break;
                case (int)ArchiveTableEnum.OliSearch_Zonghe_Vol_File:
                    tempFile = "综合卷内件.xls";
                    break;
                case (int)ArchiveTableEnum.OliSearchK02:
                    tempFile = "勘探（单井）.xls";
                    break;
                case (int)ArchiveTableEnum.Technology:
                    tempFile = "科研.xls";
                    break;
                case (int)ArchiveTableEnum.Equipment:
                    tempFile = "设备.xls";
                    break;
                case (int)ArchiveTableEnum.AudioVedioS01:
                    tempFile = "声像视频.xls";
                    break;
                case (int)ArchiveTableEnum.AudioVedioS02:
                    tempFile = "声像音频.xls";
                    break;
                case (int)ArchiveTableEnum.AudioVedioS03:
                    tempFile = "声像图像.xls";
                    break;
                case (int)ArchiveTableEnum.RealKindR01:
                    tempFile = "荣誉.xls";
                    break;
                case (int)ArchiveTableEnum.RealKindR02:
                    tempFile = "荣誉.xls";
                    break;
                case (int)ArchiveTableEnum.RealKindR03:
                    tempFile = "荣誉.xls";
                    break;
                case (int)ArchiveTableEnum.RealKindR04:
                    tempFile = "荣誉.xls";
                    break;

            }
            templetePath = Path.Combine(templetePath, tempFile);
            return templetePath;
        }
    }
}